package com.crawler;

import java.io.IOException;

import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.log4j.Logger;

public class HttpClientHelper {
	static boolean isAgent=false;
	static Logger log=Logger.getLogger(HttpClientHelper.class);
//	static String charset= "iso-8859-1";
	static String charset= "UTF-8";
	static String[] userAgents={"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31",
			"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 LBBROWSER",
			"Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.15) Gecko/20110303 Firefox/3.6.15"
	};
	static int userAgent=0;
	static HttpClient client;
	static int timeout=80000;
	private static void initClient(){
		if(client!=null)return;
		MultiThreadedHttpConnectionManager httpClientManager = new MultiThreadedHttpConnectionManager();
        HttpConnectionManagerParams params = httpClientManager.getParams();
        params.setStaleCheckingEnabled(true);
        params.setMaxTotalConnections(2);
        params.setDefaultMaxConnectionsPerHost(2);
        params.setConnectionTimeout(timeout);
        params.setSoTimeout(timeout);
		client=new HttpClient(httpClientManager);
		client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
		client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
	}
	public synchronized static String getHtml(String url){
		initClient();
		HttpMethod method = new PostMethod(url);
		method.addRequestHeader("Content-Type","text/html;charset="+charset);
		if(isAgent){
			userAgent=(userAgent+1)%3;
			method.setRequestHeader("User-Agent",userAgents[userAgent]);
		}
//		else{
//			method.setRequestHeader("User-Agent","Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1");
//		}
		method.setRequestHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		method.setRequestHeader("Accept-Encoding","gzip, deflate, sdch");
		method.setRequestHeader("Accept-Language","zh-CN,zh;q=0.8");
		method.setRequestHeader("Cache-Control","max-age=0");
		method.setRequestHeader("Connection","keep-alive");
		method.setRequestHeader("Host","118.178.168.146:58101");
		method.setRequestHeader("Upgrade-Insecure-Requests","1");
		method.setRequestHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36");
		Header[] headers = method.getRequestHeaders();
		String html=null;
		try {
			client.executeMethod(method);
			System.out.println(method.getStatusLine());
			if(method.getStatusLine().getStatusCode()==200){
				html=method.getResponseBodyAsString();
				try {
					Thread.sleep(500);	//增加等待时间，避免爬虫被屏蔽
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				log.debug(html);
			}
		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//释放连接
	        method.releaseConnection();
		}

		
		return html;
	}
}
